import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    ElementRef,
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PipelineListDto,
    OrganizationUnitServiceProxy,
    ComboboxItemDto,
    PipelineNodeServiceProxy,
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { appModuleAnimation } from '@shared/animations/routerTransition';

@Component({
    selector: 'rw5',
    templateUrl: './rw5.component.html',
    styleUrls: ['./rw5.component.less'],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()],
})
export class RW5ModalComponent extends AppComponentBase {
    @ViewChild('teamGroupCombobox') teamGroupCombobox: ElementRef;

    saving = false;

    pipe: PipelineListDto = new PipelineListDto();

    teamGroups: ComboboxItemDto[] = [];
    teamGroupName: ComboboxItemDto;

    constructor(
        injector: Injector,
        private _organizationUnitServiceProxy: OrganizationUnitServiceProxy,
        private _taskServiceProxy: PipelineNodeServiceProxy,
        private _organization: OrganizationUnitServiceProxy
    ) {
        super(injector);
    }

    load(pipe: PipelineListDto): void {
        this.pipe = pipe;
        this._taskServiceProxy.getOutplant(pipe.id).subscribe((result) => {
            this.teamGroups = result.groups;
        });
    }

    save(): void {
        this.saving = true;
        this._taskServiceProxy
            .boundingOutTeamGroup(this.pipe.id, this.teamGroupName.value)
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe((result) => {
                this.notify.success('绑定成功');
            });
    }
}
